/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_framework/zhiqim_kernel.htm
 *
 * Zhiqim Kernel is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.kernel.util;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Locale;

import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.constants.AsciiConstants;

/**
 * 字符串静态扩展类
 * 
 * @version v1.0.0 @author zouzhigang 2014-2-27 新建与整理
 */
@AnAlias("Stringx")
public class Stringx implements AsciiConstants
{
    public static int length(String s)
    {
        return (s == null)?0:s.length();
    }
    
    public static boolean isEmpty(String s)
    {
        return (s == null || s.isEmpty());
    }
    
    public static char charAt(String s, int index)
    {
        return s.charAt(index);
    }
    
    public static int codePointAt(String s, int index)
    {
        return s.codePointAt(index);
    }
    
    public static int codePointBefore(String s, int index)
    {
        return s.codePointBefore(index);
    }
    
    public static int codePointCount(String s, int beginIndex, int endIndex)
    {
        return s.codePointCount(beginIndex, endIndex);
    }
    
    public static int offsetByCodePoints(String s, int index, int codePointOffset)
    {
        return s.offsetByCodePoints(index, codePointOffset);
    }
    
    public static void getChars(String s, int srcBegin, int srcEnd, char dst[], int dstBegin)
    {
        s.getChars(srcBegin, srcEnd, dst, dstBegin);
    }
    
    public static boolean equals(String s, String a)
    {
        return (s == null)?(a == null):s.equals(a);
    }
    
    public static boolean contentEquals(String s, StringBuffer sb)
    {
        return (s == null)?(sb == null):s.contentEquals(sb);
    }
    
    public static boolean contentEquals(String s, CharSequence cs)
    {
        return (s == null)?(cs == null):s.contentEquals(cs);
    }
    
    public static int compareTo(String s, String a)
    {
        return s.compareTo(a);
    }
    
    public static int compareToIgnoreCase(String s, String a)
    {
        return s.compareToIgnoreCase(a);
    }

    public static boolean regionMatches(String s, int toffset, String other, int ooffset, int len) 
    {
        return (s == null)?(other == null):s.regionMatches(toffset, other, ooffset, len);
    }
    
    public static boolean regionMatches(String s, boolean ignoreCase, int toffset, String other, int ooffset, int len) 
    {
        return (s == null)?(other == null):s.regionMatches(ignoreCase, toffset, other, ooffset, len);
    }
    
    public static boolean startsWith(String s, String prefix, int toffset)
    {
        return (s == null)?(prefix == null):s.startsWith(prefix, toffset);
    }
    
    public static boolean startsWith(String s, String prefix)
    {
        return (s == null)?(prefix == null):s.startsWith(prefix);
    }
    
    public static boolean endsWith(String s, String suffix)
    {
        return (s == null)?(suffix == null):s.endsWith(suffix);
    }
    
    public static int hashCode(String s) 
    {
        return (s == null)?0:s.hashCode();
    }
    
    public static int indexOf(String s, int ch) 
    {
        return (s == null)?-1:s.indexOf(ch);
    }
    
    public static int indexOf(String s, int ch, int fromIndex) 
    {
        return (s == null)?-1:s.indexOf(ch, fromIndex);
    }
    
    public static int lastIndexOf(String s, int ch) 
    {
        return (s == null)?-1:s.lastIndexOf(ch);
    }
    
    public static int lastIndexOf(String s, int ch, int fromIndex) 
    {
        return (s == null)?-1:s.lastIndexOf(ch, fromIndex);
    }
    
    public static int indexOf(String s, String str)
    {
        return (s == null)?-1:s.indexOf(str);
    }
    
    public static int indexOf(String s, String str, int fromIndex)
    {
        return (s == null)?-1:s.indexOf(str, fromIndex);
    }
    
    public static int lastIndexOf(String s, String str)
    {
        return (s == null)?-1:s.lastIndexOf(str);
    }
    
    public static int lastIndexOf(String s, String str, int fromIndex)
    {
        return (s == null)?-1:s.lastIndexOf(str, fromIndex);
    }
    
    public static String substring(String s, int beginIndex) 
    {
        return s.substring(beginIndex);
    }
    
    public static String substring(String s, int beginIndex, int endIndex)
    {
        return s.substring(beginIndex, endIndex);
    }
    
    public static CharSequence subSequence(String s, int beginIndex, int endIndex) 
    {
        return s.subSequence(beginIndex, endIndex);
    }
    
    public static String concat(String s, String str)
    {
        return s.concat(str);
    }
    
    public static String replace(String s, char oldChar, char newChar) 
    {
        return s==null?s:s.replace(oldChar, newChar);
    }
    
    public static boolean matches(String s, String regex)
    {
        return (s == null)?(regex == null):s.matches(regex);
    }
    
    public static boolean contains(String s, CharSequence cs) 
    {
        return (s == null)?(cs == null):s.contains(cs);
    }
    
    public static String replaceFirst(String s, String regex, String replacement)
    {
        return s.replaceFirst(regex, replacement);
    }
    
    public static String replaceAll(String s, String regex, String replacement)
    {
        return s.replaceAll(regex, replacement);
    }
    
    public static String replace(String s, CharSequence target, CharSequence replacement) 
    {
        return s.replace(target, replacement);
    }
    
    public static String[] split(String s, String regex, int limit) 
    {
        return s.split(regex, limit);
    }
    
    public static String[] split(String s, String regex)
    {
        return s.split(regex);
    }
    
    public static String toLowerCase(String s, Locale locale) 
    {
        return s==null?s:s.toLowerCase(locale);
    }
    
    public static String toLowerCase(String s) 
    {
        return s==null?s:s.toLowerCase();
    }

    public static String toUpperCase(String s, Locale locale) 
    {
        return s==null?s:s.toUpperCase(locale);
    }
    
    public static String toUpperCase(String s)
    {
        return s==null?s:s.toUpperCase();
    }
    
    public static String trim(String s)
    {
        if (s == null || s.isEmpty())
            return s;
        
        //先去除左边空白,12288是中文大写空格,65279是BOM头空格
        int len = s.length(), i = 0;
        while ((i < len) && Validates.isWhitespace(s.charAt(i)))
            i++;
        s = (i>0)?s.substring(i):s;
        
        //再去除右边空白
        len = s.length(); i = len-1;
        while ((i > 0) && Validates.isWhitespace(s.charAt(i)))
            i--;
        
        return (i<len-1)?s.substring(0, i+1):s;
    }
    
    public static String toString(Object o) 
    {
        return (o == null)?null:o.toString();
    }
    
    public static char[] toCharArray(String s) 
    {
        return s.toCharArray();
    }
    
    public static boolean equalsIgnoreCase(String s, String a)
    {
        return (s == null)?(a == null):s.equalsIgnoreCase(a);
    }
    
    public static byte[] getBytes(String s)
    {
        return s.getBytes();
    }
    
    public static byte[] getBytes(String s, String charsetName)
    {
        try{return s.getBytes(charsetName);}catch (UnsupportedEncodingException e){return null;}
    }

    public static byte[] getBytes(String s, Charset charset)
    {
        return s.getBytes(charset);
    }

    public static String intern(String s)
    {
        return s.intern();
    }
    
    /********************************************/
    //以下为String的静态方法
    /********************************************/
    
    /**
     * 格式化，有%s, %d, %f等
     * 
     * @param format    格式，比如 "您已成功注册到%s，用户名：%s，密码：%s，请登录..."
     * @param args      参数列表，比如 {"知启蒙","张三","111111"};
     * @return          格式化后的内容，例子的内容则为： "您已成功注册到知启蒙，用户名：张三，密码：111111，请登录..."
     */
    public static String format(String format, Object... args) 
    {
        return String.format(format, args);
    }

    public static String format(Locale l, String format, Object... args) 
    {
        return String.format(l, format, args);
    }

    public static String valueOf(Object obj) 
    {
        return String.valueOf(obj);
    }

    public static String valueOf(char data[]) 
    {
        return String.valueOf(data);
    }

    public static String valueOf(char data[], int offset, int count) 
    {
        return String.valueOf(data, offset, count);
    }

    public static String copyValueOf(char data[], int offset, int count) 
    {
        return String.copyValueOf(data, offset, count);
    }

    public static String copyValueOf(char data[]) 
    {
        return String.copyValueOf(data);
    }

    public static String valueOf(boolean b) 
    {
        return String.valueOf(b);
    }

    public static String valueOf(char c) 
    {
        return String.valueOf(c);
    }

    public static String valueOf(int i) 
    {
        return String.valueOf(i);
    }

    public static String valueOf(long l) 
    {
        return String.valueOf(l);
    }

    public static String valueOf(float f) 
    {
        return String.valueOf(f);
    }

    public static String valueOf(String s, double d) 
    {
        return String.valueOf(d);
    }
}
